<?php
/**
 * Description of NavigationMenu
 *
 * @author gun
 */
class CHelper_View_Helper_NavigationMenu extends Zend_View_Helper_Abstract {
    
    public function setView(\Zend_View_Interface $view) {
        parent::setView($view);
    }
    
    public function NavigationMenu($position = '', $group = '', $options = array()) {
        if (isset($options) && is_array($options)) {
            foreach($options as $key => $value) {
                
            }
        }
        
        $where = array('a.groupID = ?' => $group, 'b.gmenuID = ?' => $position, 'a.status = ?' => true);
        
        $service = AppRegistry::loadService('Admin_Model_MenuService');
        $data = $service->findAll($where);

        $menu = '<ul class="menu">';
        $url = "";
        
        $session = new Zend_Session_Namespace(Admin_SecureController::$_NS);
        $front = Zend_Controller_Front::getInstance();
        
        foreach($data as $value) {
            if ('0' === $value['parentID']) {
                $url = (empty($value['link']) ? '#' : $front->getBaseUrl() . '/' . $front->getRequest()->getModuleName() . '/' . $value['link']);
                $icon = (empty($value['icon']) ? '' : '<i class="' . $value['icon'] .'"></i>');
                $menu .= '<li><a href="' . $url .'">' . $icon . ''. $value['name'] .'</a>';
                
                $child = $service->getChildMenu($value['groupID'], $value['menuID']);
//                                echo "<pre>", print_r($child), "</pre>";
                if ($child) {
                    $menu .= '<ul>';
                    foreach ($child as $childItem) {
                        $icon2 = (empty($childItem['icon']) ? '' : '<i class="' . $childItem['icon'] .'"></i>');
                        $menu .= '<li>' . ('1' === $childItem['status'] ? '<a href="' . $front->getBaseUrl() . '/' . $front->getRequest()->getModuleName() . '/' . $childItem['link'] . '">' . $icon2 . ''. $childItem['name'] .'</a>' : '') .'</li>';
                    }
                    
                    $menu .= '</ul></li>';
                    
                } else {
                    $menu .= '</li>';

                }
            }
        }
        
        // Add Username and Logout button
        $menu .= '<li class="right">
                    <a href="' . $front->getBaseUrl() . '/' . $front->getRequest()->getModuleName() . '/login/logout' . '"><i class="icon-signout"></i> Logout</a></li>                
                    <li class="right"><a href="#">Welcome, ' . $session->user->name . '</a></li>';
        
        $menu .= '</ul>';
        
        return $menu;
    }
    
}

?>
